package com.itheima.controller;

import com.itheima.anno.LogOperation;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;


    /***
     * 违纪操作
     * @param id
     * @param score
     * @return
     */
    @LogOperation
    @PutMapping("/violation/{id}/{score}")
    public Result updateByIdAndScore(@PathVariable Integer id,@PathVariable Integer score){
        studentService.updateByIdAndScore(id,score);
        return Result.success();
    }


    /**
     * 删除学员
     * @param ids
     * @return
     */
    @LogOperation
    @DeleteMapping("/{ids}")
    public Result deleteStudent(@PathVariable Integer[] ids){
        studentService.deleteStudent(ids);
        return Result.success();
    }

    /***
     * 修改学员
     * @param student
     * @return
     */
    @LogOperation
    @PutMapping
    public Result updateStudent(@RequestBody Student student){
        studentService.updateStudent(student);
        return Result.success();
    }

    /***
     * 根据ID查询学员
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getStudentById(@PathVariable Integer id){
        Student student = studentService.getStudentById(id);
        return Result.success(student);
    }

    /***
     * 分页查询
     * @param studentQueryParam
     * @return
     */
    @GetMapping
    public Result pageStudent(StudentQueryParam studentQueryParam){
        PageResult pageResult = studentService.pageStudent(studentQueryParam);
        return Result.success(pageResult);
    }


    /***
     * 添加学员
     */
    @LogOperation
    @PostMapping
    public Result insertStudent(@RequestBody Student student){
        studentService.insertStudent(student);
        return Result.success();
    }

}
